package cn.beyond.vehicleManagement.controller;
import cn.beyond.vehicleManagement.pojo.SysDepartment;
import cn.beyond.vehicleManagement.pojo.SysUser;
import cn.beyond.vehicleManagement.service.UserService;
import cn.beyond.vehicleManagement.util.PageUtil;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = {"/userServlet"})
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //定义全局变量
        UserService userService=new UserService();
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SysUser user=new SysUser();
        Map<String,Object>map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<SysUser> userList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
            case "addUser":
              boolean addflag= addUser(req,userService);
              out.print(addflag);
                break;
            case "deleteUser":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
               int delcount=userService.deleteUser(delId,"id");
                System.out.println(delcount);
               if(delcount>0){
                   delflag=true;
               }
               out.print(delflag);


                break;
            case "updateUser":
                updateUser(req,userService,out);

                break;
            case "selectUser":

                break;
            case"checkUser":
                //检查用户名和密码
                String username=req.getParameter("username");
                String pwd=req.getParameter("pwd");
                map.put("login_name",username);
                map.put("password",pwd);
                SysUser user1=userService.selectUserByOne(user,map);
               // System.out.println(user1);
                boolean isExists=false;
                if(user1.getId()!=null){
                    isExists=true;
                }
                out.print(isExists);
                out.flush();
                map.clear();
                break;
            case "listUser":
                //列举出用户列表
                int startRow= Integer.parseInt(req.getParameter("startRow"));
                int pageSize= Integer.parseInt(req.getParameter("pageSize"));
                //返回用户集合
                userList= PageUtil.getPageUtil(startRow,pageSize,"sys_user",user);
                int totals=PageUtil.getPageCount("sys_user");
                int totalpage=totals%pageSize==0?(totals/pageSize):(totals/pageSize+1);
                List<SysDepartment>departments=new ArrayList<>();
                SysDepartment sysDepartment=new SysDepartment();
                departments=PageUtil.getPageUtil(0,100,"sys_department",sysDepartment);
                //保存总信息
                List<Integer>totalinfo=new ArrayList<>();
                totalinfo.add(totals);
                totalinfo.add(totalpage);
                //用map集合保存信息
                 List<Object>listListMap=new ArrayList<>();
                 listListMap.add(totalinfo);
                 listListMap.add(userList);
                 listListMap.add(departments);
                 String listJson=JSON.toJSONString(listListMap);
                out.print(listJson);
                break;
                //查看用户详情
            case "detailUser":
                detailUser(req,map,userService,user,out);
//                String id=req.getParameter("Id");
//                map.put("id",id);
//            SysUser user2=userService.selectUserByOne(user,map);
//            String detailJosn=JSON.toJSONString(user2);
//            out.write(detailJosn);
//            map.clear();
            break;
            case "back":
                req.getSession().invalidate();
                resp.sendRedirect("pages/login.jsp");
                break;
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }

    /**
     * 显示用户详请
     * @param req
     * @param map
     * @param userService
     * @param user
     * @param out
     */
    protected void detailUser(HttpServletRequest req,Map<String,Object> map,
                              UserService userService,SysUser user,PrintWriter out){
        String id=req.getParameter("Id");
        map.put("id",id);
        SysUser user2=userService.selectUserByOne(user,map);
        String detailJosn=JSON.toJSONString(user2);
        out.write(detailJosn);
        map.clear();
    }

    /**
     * 增加员工
     * @param req
     * @param userService
     */
     public boolean addUser(HttpServletRequest req, UserService userService){
         int department= Integer.parseInt(req.getParameter("department"));
        String login_name=req.getParameter("login_name");
        String real_name=req.getParameter("real_name");
        String password=req.getParameter("password");
        String Email=req.getParameter("Email");
        String tel=req.getParameter("tel");
        int sex= Integer.parseInt(req.getParameter("sex"));
        int role= Integer.parseInt(req.getParameter("role"));
         String address=req.getParameter("address");
         SysUser user=new SysUser(null,department,login_name,real_name,Email,tel,
                 sex,address,password,1,0);
         System.out.println(user);
          int addCount=userService.addUser(user);
          boolean flag=false;
           if(addCount>0){
               flag=true;
           }else{
               flag=false;
           }
         System.out.println(flag);
          return  flag;
     }

    /**
     * 更新員工
     * @param req
     * @param userService
     * @param out
     */
     public void updateUser(HttpServletRequest req, UserService userService,PrintWriter out){
         int id= Integer.parseInt(req.getParameter("id"));
         int department= Integer.parseInt(req.getParameter("department"));
         String login_name=req.getParameter("login_name");
         String real_name=req.getParameter("real_name");
         String password=req.getParameter("password");
         String Email=req.getParameter("Email");
         String tel=req.getParameter("tel");
         int sex= Integer.parseInt(req.getParameter("sex"));
         int role= Integer.parseInt(req.getParameter("role"));
         String address=req.getParameter("address");
         int status= Integer.parseInt(req.getParameter("status"));
         int delflag= Integer.parseInt(req.getParameter("delflag"));
         SysUser user=new SysUser(id,department,login_name,real_name,Email,tel
                 ,sex,address,password,status,delflag);
         int addCount=userService.update(user);
         System.out.println(addCount);
         boolean flag=false;
         if(addCount>0){
             flag=true;
         }else{
             flag=false;
         }
         System.out.println(flag);
         out.print(flag);
     }
}
